package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class XMLMappedBuilder {
    private Configuration configuration;

    public XMLMappedBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        // 根路径已经获取到，现在可以获取里面的属性
        String namespace = rootElement.attributeValue("namespace");

        // 封装成MapList 针对不同的sql类型进行判断
        HashMap<SqlType, List<Element>> sqlTypeListHashMap = new HashMap<>();

        // 获取CRUD操作标签
        List<Element> selectList = rootElement.selectNodes("//select");
        List<Element> updateList = rootElement.selectNodes("//update");
        List<Element> insertList = rootElement.selectNodes("//insert");
        List<Element> deleteList= rootElement.selectNodes("//delete");


        sqlTypeListHashMap.put(SqlType.SELECT, selectList);
        sqlTypeListHashMap.put(SqlType.UPDATE, updateList);
        sqlTypeListHashMap.put(SqlType.INSERT, insertList);
        sqlTypeListHashMap.put(SqlType.DELETE, deleteList);


        sqlTypeListHashMap.entrySet()
                .stream()
                .forEach(sqlType2List -> {
                    SqlType sqlType = sqlType2List.getKey();
                    if(SqlType.SELECT.equals(sqlType)){
                        List<Element> elementList = sqlType2List.getValue();
                        setMappedStatement(elementList, namespace, SqlType.SELECT);
                    }else if(SqlType.UPDATE.equals(sqlType)){
                        List<Element> elementList = sqlType2List.getValue();
                        setMappedStatement(elementList, namespace, SqlType.UPDATE);
                    }else if(SqlType.INSERT.equals(sqlType)){
                        List<Element> elementList = sqlType2List.getValue();
                        setMappedStatement(elementList, namespace, SqlType.INSERT);
                    }else if(SqlType.DELETE.equals(sqlType)){
                        List<Element> elementList = sqlType2List.getValue();
                        setMappedStatement(elementList, namespace, SqlType.DELETE);
                    }
                });




    }

    /**
     * 设置MappedStatement
     * @param elementList
     */
    private void setMappedStatement(List<Element> elementList, String namespace, SqlType sqlType){
        for (Element element : elementList) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterTpye = element.attributeValue("paramterTpye");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamType(paramterTpye);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);
            mappedStatement.setSqlType(sqlType);
            String statementId= namespace+"."+id;
            // 需要获取statementid :namespace.id
            configuration.getMappedStatementMap().put(statementId, mappedStatement);
        }
    }
}
